<?php
/*
	申请成为代理商
	如果已申请，但尚未审核通过，则页面显示请耐心等待管理员审核！
	如果已申请，且审核通过，则显示审核通过，并提供按钮前往代理中心
	如果审核不通过，则显示审核不通过原因
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$return['ischecklogin']=model('jy_weishop/member')->checklogin('','','','',true);
	$member=$this->pdo_get('member',array('mid'=>$_SESSION['uid']));
	$return['applystatus']=$this->getcolumn('apply',array('mid'=>$_SESSION['uid']),'status');
	if($return['applystatus']==1 && $member['is_agent']!=1){
		pdo_delete('jy_leijifee_apply',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	}
	$apply=$this->get('apply',array('mid'=>$_SESSION['uid']),array('id','remark'));
	if($apply['id']){
		$return['apply']=$apply;
		$return['applystatus']=$this->getcolumn('apply',array('mid'=>$_SESSION['uid']),'status');
	}
	//判断是否满足申请分销商条件
	$can_apply=true;
	if($this->setting['become']==2){
				//检查消费次数
				$num=pdo_count('jy_leijifee_order',array('mid'=>$member['mid'],'ispay'=>1));
				if($num<$this->setting['num']){
					$can_apply=false;
					$return['now']=$num;
					$return['limit']=$this->setting['num'];
					$return['unit']='次';
				}
	}
	if($this->setting['become']==3){
				//检查消费次数
				$money=pdo_getsum('jy_leijifee_order',array('mid'=>$member['mid'],'ispay'=>1),'price');
				if($money<$this->setting['money']){
					$can_apply=false;
					$return['now']=price($money);
					$return['limit']=price($this->setting['money']);
					$return['unit']='元';
				}
	}
	$return['can_apply']=$can_apply;
	$return['member']=$member;
	if($member['agent_id']){
		$return['agent']=model('core/member')->detail($member['agent_id'],array('nickname','avatar'));
		$return['agent_name']=$return['agent']['nickname'];
	}
	$setting=$this->plugin_setting('page_setting');
	$tagent=$setting['tagent']?:'代理商';
	set_html('title','申请成为'.$tagent);
	$return['setting']=$setting;
	$return['member']=$member?:false;
	$return['item']=model('core/member')->detail($_SESSION['uid'],array('nickname','avatar','realname','mobile','wechat'));
	$return['item']['diyformfields']=$setting['diyformfields']?:array();
	exi($return);
}
if($_RQ['op']=='apply'){
	$post=get_postdata();
	if(!$post['agree']){
		exi('请先阅读协议！','error');
	}
	if(!$post['realname']){
		exi('请输入姓名！','error');
	}
	if(!$post['mobile']){
		exi('请输入联系电话！','error');
	}
	if(!preg_match("/^1[345678]{1}\d{9}$/",$post['mobile'])){  	 
			exi('请输入正确的手机号码','error');
	}
	if($post['diyformfields']){
			foreach ($post['diyformfields'] as $item) {
				if($item['ismust']==1 && empty($item['data'])){
					exi('请完善'.$item['title'].'内容后再提交','error');
				}
			}
	}
	$member_update=array(
		'realname'=>trim($post['realname']),
		'mobile'=>trim($post['mobile']),
		'diyformdata'=>ijson_encode($post['diyformfields']),
	);
	if($post['wechat']){
		$member_update['wechat']=trim($post['wechat']);
	}
	$this->model('member')->member_update($_SESSION['uid'],$member_update);	
	$this->model('member')->apply_agent($_SESSION['uid']);
	exi('申请成功！请耐心等待管理员审核！','','main');
}